Practical Lab 2 - Data Visualization and Publication¶

Graph Creation¶

Matplotlib¶

In [1]:
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm

plt.style.use('_mpl-gallery')

n_radii = 8
n_angles = 36

# Make radii and angles spaces
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]

# Convert polar (radii, angles) coords to cartesian (x, y) coords.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

# Plot
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_trisurf(x, y, z, vmin=z.min() * 2, cmap=cm.Blues)

ax.set(xticklabels=[],
       yticklabels=[],
       zticklabels=[])

plt.show()

Example of matplotlib plot_trisurf

Seaborn¶

In [2]:
import seaborn as sns
sns.set_theme(style="whitegrid")

# Load the example planets dataset
planets = sns.load_dataset("planets")

cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
g = sns.relplot(
    data=planets,
    x="distance", y="orbital_period",
    hue="year", size="mass",
    palette=cmap, sizes=(10, 200),
)
g.set(xscale="log", yscale="log")
g.ax.xaxis.grid(True, "minor", linewidth=.25)
g.ax.yaxis.grid(True, "minor", linewidth=.25)
g.despine(left=True, bottom=True)
Out[2]:
<seaborn.axisgrid.FacetGrid at 0x27c7fbec450>

Example of seaborn Scatterplot

Plotly Express¶

In [9]:
import plotly.express as px
import plotly.offline as offline
offline.init_notebook_mode()

df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(df, path=['continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'])
fig.show()

A simple example of Plotly

Graphing Libraries icon
Matplotlib Matplotlib makes easy things easy and hard things possible.
Seaborn Seaborn is a Python data visualization library based on matplotlib.
Plotly Express Interactive charts and maps for Python, R, Julia, Javascript, ggplot2, F#, MATLABĀ®, and Dash.

Matplotlib Seaborn Plotly Express